<?php
namespace app\admin\model\agent;
use app\admin\model\agent\User as AgentUser;
use app\admin\model\BaseSetting;
use app\admin\model\admin\Agent as AgentModel;
use think\Db;
class Agent extends AgentModel
{
    /**
     * 获取代理商信息
     * @param int|null $agentId
     * @throws \think\exception\DbException
     */
    public static function detail($agentId = null)
    {
        return static::get($agentId ?: []);
    }


    /**
     * 获取代理商列表
     * @param boolean $is_recycle
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($is_recycle = false)
    {
        return $this->useGlobalScope(false)->where('is_recycle', '=', (int)$is_recycle)
            ->where('is_delete', '=', 0)
            ->order(['create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => request()->request()
            ]);

    }


    /**
     * 新增记录
     * @param $data
     * @return bool|mixed
     */
    public function add($data)
    {

        if ($data['password'] !== $data['password_confirm']) {
            $this->error = '确认密码不正确';
            return false;
        }
        if (AgentUser::checkExist($data['user_name'])) {
            $this->error = '代理商用户名已存在';
            return false;
        }
        return $this->transaction(function () use ($data) {
            // 添加小程序记录
            $this->allowField(true)->save($data);
            // 商城默认设置
           // (new Setting)->insertDefault($this['agent_id'], $data['agent_name']);
            // 新增商家用户信息
            (new AgentUser)->add($this['agent_id'], $data);
            return true;
        });
    }


    /**
     * 激活代理商
     * @param bool $agent_id
     * @return false|int
     */
    public function actived($agent_id)
    {

        $data=$this->get($agent_id);
        // 激活代理商,0,城市承运商，1.城市运营商，2.城市合伙人
        if($data['type']==1){
            $finishTime=strtotime("+3 year");
            $cover=BaseSetting::getItemValue('operate_template');

        }else if($data['type']==2){
            $finishTime=strtotime("+2 year");
            $cover=BaseSetting::getItemValue('partner_template');
        }else{
            $finishTime=strtotime("+5 year");
            $cover=0;
        }
        return $this->transaction(function () use ($agent_id,$finishTime,$cover) {
            Db::name('agent')->where(['agent_id'=>$agent_id])->update(['active' =>1,'finish_time'=>$finishTime,'cover'=>$cover]);
            // 激活商家用户信息
            (new AgentUser)->save(['active' =>1],['agent_id'=>$agent_id]);
            return true;
        });

    }


    /**
     * 移入移出回收站
     * @param bool $is_recycle
     * @return false|int
     */
    public function recycle($is_recycle = true)
    {
        return $this->save(['is_recycle' => (int)$is_recycle]);
    }

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        return $this->transaction(function () {
            // 删除商家用户信息
            (new AgentUser)->setDelete($this['agent_id']);
            // 设置当前商城为已删除
            return $this->save(['is_delete' => 1]);
        });
    }



}